package com.black.knight.chess;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ZoomControls;

import com.black.knight.chess.model.SahModel;
import com.black.knight.chess.model.SettingsModel;
import com.black.knight.chess.plugins.BluetoothPlugin;
import com.black.knight.chess.utils.Utils;

public class SettingsActivity extends Activity{

	private SettingsActivity context;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        
    	this.context = this;
    	
    	Utils.setLanguage(SettingsModel.getInstance().getLanguage(), context);
    	
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.settings);
        
        setTitle(context.getResources().getString(R.string.settings));
        
        final CheckBox autoRotateCheckBox = (CheckBox) findViewById(R.id.autoRotateCheckBox);
        final CheckBox markFieldsCheckBox = (CheckBox) findViewById(R.id.markFieldsCheckBox);
        final CheckBox markLastMoveCheckBox = (CheckBox) findViewById(R.id.markLastMoveCheckBox);
        final CheckBox playOnClockCheckBox = (CheckBox) findViewById(R.id.playOnClockCheckBox);
        final CheckBox animateMoveCheckBox = (CheckBox) findViewById(R.id.moveAnimationCheckBox);
        final CheckBox recognizeGameCheckBox = (CheckBox) findViewById(R.id.recognizeGameCheckBox);
        final CheckBox showEvaluationCheckBox = (CheckBox) findViewById(R.id.showEvaluationCheckBox);
        final CheckBox showRecommendMoveCheckBox = (CheckBox) findViewById(R.id.showRecommendMoveCheckBox);
        final EditText gameDurationField = (EditText) findViewById(R.id.gameDurationField);
        final Button chooseLanguageBtn = (Button) findViewById(R.id.chooseLanguageBtn);
        final EditText playerNameField = (EditText) findViewById(R.id.playerNameField);
        final CheckBox playSoundCheckBox = (CheckBox) findViewById(R.id.playSoundCheckBox);
        final ZoomControls tableSizeControls = (ZoomControls) findViewById(R.id.tableSizeControls);
        final EditText tableSizeField = (EditText) findViewById(R.id.tableSizeField);
        
        autoRotateCheckBox.setChecked(SettingsModel.getInstance().isAutoRotate());
        markFieldsCheckBox.setChecked(SettingsModel.getInstance().isMarkFields());
        playOnClockCheckBox.setChecked(SettingsModel.getInstance().isPlayOnClock());
        markLastMoveCheckBox.setChecked(SettingsModel.getInstance().isMarkLastMove());
        animateMoveCheckBox.setChecked(SettingsModel.getInstance().isAnimateMove());
        recognizeGameCheckBox.setChecked(SettingsModel.getInstance().isRecognizeGame());
        showEvaluationCheckBox.setChecked(SettingsModel.getInstance().isShowEvaluation());
        showRecommendMoveCheckBox.setChecked(SettingsModel.getInstance().isRecommendMove());
        playSoundCheckBox.setChecked(SettingsModel.getInstance().isPlaySound());
        gameDurationField.setText(String.valueOf(SettingsModel.getInstance().getGameDuration()));
        tableSizeField.setText(String.valueOf(SettingsModel.getInstance().getTableSize()));
        
        String lang = context.getResources().getString(R.string.english_text);
        
        if("ba".equals(SettingsModel.getInstance().getLanguage())) {
        	lang = context.getResources().getString(R.string.bosnian_text);
        }
        
        chooseLanguageBtn.setText(context.getResources().getString(R.string.choose_language) + " (" + lang + ")");
        
        tableSizeControls.setOnZoomInClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int tableSize = Integer.valueOf(tableSizeField.getText().toString());
				
				if(tableSize < 10) {
					tableSize++;
					SettingsModel.getInstance().setTableSize(tableSize);
					
					tableSizeField.setText(String.valueOf(SettingsModel.getInstance().getTableSize()));
				}	
			}
		});
        
        tableSizeControls.setOnZoomOutClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int tableSize = Integer.valueOf(tableSizeField.getText().toString());
				
				if(tableSize > 1) {
					tableSize--;
					SettingsModel.getInstance().setTableSize(tableSize);
					
					tableSizeField.setText(String.valueOf(SettingsModel.getInstance().getTableSize()));
				}	
			}
		});
        
        if(SettingsModel.getInstance().getPlayerName().length() == 0 && BluetoothPlugin.getInstance().getmBluetoothAdapter() != null) {
        	if(SettingsModel.getInstance().getPlayerName().length() == 0) {
        		BluetoothAdapter adapter = BluetoothPlugin.getInstance().getmBluetoothAdapter();
        		if(adapter != null && adapter.getName() != null) {
        			playerNameField.setText(adapter.getName());
        		} else {
        			playerNameField.setText("Player");		
        		}
        	}
        } else {
        	playerNameField.setText(SettingsModel.getInstance().getPlayerName());
        }
        
        gameDurationField.setEnabled(SettingsModel.getInstance().isPlayOnClock());
        
        recognizeGameCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SettingsModel.getInstance().setRecognizeGame(isChecked);
			}
		});
        
        showEvaluationCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SettingsModel.getInstance().setShowEvaluation(isChecked);
				SahModel.context.refreshHeader();
			}
		});
        
        showRecommendMoveCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SettingsModel.getInstance().setRecommendMove(isChecked);
			}
		});
        
        autoRotateCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SahModel.getInstance().setAutoRotate(isChecked);
				if(!isChecked) {
					SahModel.context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
				}
			}
		});
        
        animateMoveCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SettingsModel.getInstance().setAnimateMove(isChecked);
			}
		});
        
        markFieldsCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SettingsModel.getInstance().setMarkFields(isChecked);
			}
		});
        
        markLastMoveCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SettingsModel.getInstance().setMarkLastMove(isChecked);
			}
		});
        
        playSoundCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SettingsModel.getInstance().setPlaySound(isChecked);
			}
		});
        
        playOnClockCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SettingsModel.getInstance().setPlayOnClock(isChecked);
				gameDurationField.setEnabled(SettingsModel.getInstance().isPlayOnClock());
				
				if(isChecked) {
					gameDurationField.requestFocus();
					gameDurationField.selectAll();	
				}
				
				SahModel.context.refreshHeader();
			}
		});
        
        gameDurationField.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(gameDurationField.getText().toString().length() > 0) {
					int duration = Integer.valueOf(gameDurationField.getText().toString());
					
					if(duration > 0) {
						SettingsModel.getInstance().setGameDuration(duration);
					}
				}
			}

			@Override
			public void afterTextChanged(Editable s) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
		});
        
        gameDurationField.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				gameDurationField.selectAll();
			}
		});
        
        playerNameField.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(playerNameField.getText().toString().length() > 0) {
					SettingsModel.getInstance().setPlayerName(playerNameField.getText().toString());
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
        
        chooseLanguageBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Utils.chooseLanguage(context);
			}
		});
    }
    
    @Override
	protected void onResume() {
		super.onResume();
		
		final CheckBox autoRotateCheckBox = (CheckBox) findViewById(R.id.autoRotateCheckBox);
		
		autoRotateCheckBox.requestFocus();
	}

	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        Utils.setLanguage(SettingsModel.getInstance().getLanguage(), context);
      
      super.onConfigurationChanged(newConfig);
    }
}